//
//  ProjectTools.m
//  Project
//
//  Created by 冯东辉 on 2018/9/11.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "ProjectTools.h"

@implementation ProjectTools


/**
 验证手机号是否合法

 @param mobileNum 手机号
 @return YES/NO
 */
+ (BOOL)isMobileNumber:(NSString *)mobileNum {
    
    if (mobileNum.length != 11) {
        
        return NO;
    }
    
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:mobileNum];
    
    /**
     * 手机号码: NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\\d{8}$";
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
     * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
     * 联通号段: 130,131,132,155,156,185,186,145,176,1709
     * 电信号段: 133,153,180,181,189,177,1700
     */
    
    /** NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
     * 中国移动：China Mobile
     * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
     */
    
    /** NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
     * 中国联通：China Unicom
     * 130,131,132,155,156,185,186,145,176,1709
     */
    
    /** NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";
     * 中国电信：China Telecom
     * 133,153,180,181,189,177,1700
     */
    
    
    /**
     NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
     NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
     NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
     NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
     
     if (([regextestmobile evaluateWithObject:mobileNum] == YES)
     || ([regextestcm evaluateWithObject:mobileNum] == YES)
     || ([regextestct evaluateWithObject:mobileNum] == YES)
     || ([regextestcu evaluateWithObject:mobileNum] == YES)) {
     
     return YES;
     }
     else {
     return NO;
     } */
}

/**
 判断是否为无效字符串
 
 @param str 字符串
 @return YES无效/NO有效
 */
+ (BOOL)isValiString:(NSString *)str {
    
    str = [NSString stringWithFormat:@"%@", str];
    if ([str isEqualToString:@"(null)"] || [str isEqualToString:@""]||[str isEqual:[NSNull null]]|| [str isEqualToString:@"nil"] || [str isEqual:nil] || [str isEqualToString:@"NULL"] || [str isEqualToString:@"Nil"] || [str isEqual:Nil] || [str isEqual:NULL]|| [str isEqualToString:@"<nil>"]|| [str isEqual:nil]|| str == nil || str.length == 0 || [str isEqualToString:@"<null>"]) {
        return YES;
    }else {
        return NO;
    }
}

/**
 获取字符串的Rect
 
 @param string 字符串
 @param font 字体
 @param size 最大size
 @return Rect
 */
+ (CGRect)getStringRectWithString:(NSString *)string
                             font:(UIFont *)font
                          maxSize:(CGSize)size {
    
    NSDictionary *dic=@{NSFontAttributeName:font};
    
    CGRect rect = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect;
}

/**
 设置Label前间距
 
 @param str str
 @param space 间距
 @return NSMutableAttributedString
 */
+ (NSMutableAttributedString *)setBeforeSpacingWithStr:(NSString *)str
                                                 space:(NSInteger)space {
    
    if ([ProjectTools isValiString:str]) {
        str = @"";
    }
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.firstLineHeadIndent = space;
    
    NSMutableAttributedString *attTtitle = [[NSMutableAttributedString alloc] initWithString:str];
    [attTtitle addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attTtitle.length)];
    
    return attTtitle;
}


/**
 设置渐变

 @param benginColor 开始颜色
 @param endColor 结束颜色
 @param frame 大小
 @param cornerRadius 圆角
 @return CAGradientLayer
 */
+ (CAGradientLayer *)setJianBianWithBenginColor:(UIColor *)benginColor
                                       endColor:(UIColor *)endColor
                                          frame:(CGRect)frame
                                   cornerRadius:(CGFloat)cornerRadius {
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)benginColor.CGColor, (__bridge id)endColor.CGColor];
    gradientLayer.locations = @[@0.0, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = frame;
    gradientLayer.cornerRadius = cornerRadius;
    
    return gradientLayer;
}

/**
 设置渐变
 
 @param benginColor 开始颜色
 @param endColor 结束颜色
 @param frame 大小
 @return CAGradientLayer
 */
+ (CAGradientLayer *)setModuleListJianBianWithBenginColor:(UIColor *)benginColor
                                                 endColor:(UIColor *)endColor
                                                    frame:(CGRect)frame {
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)benginColor.CGColor, (__bridge id)endColor.CGColor];
    gradientLayer.locations = @[@0.0, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 1.0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = frame;
    return gradientLayer;
}

/**
 设置layer渐变
 
 @param benginColor 开始颜色
 @param endColor 结束颜色
 @param layer layer
 */
+ (void)setLayerJianBianWithBenginColor:(UIColor *)benginColor
                               endColor:(UIColor *)endColor
                                  layer:(CAGradientLayer *)layer {
    
    layer.colors = @[(__bridge id)benginColor.CGColor, (__bridge id)endColor.CGColor];
    layer.locations = @[@0.0, @1.0];
    layer.startPoint = CGPointMake(0, 1.0);
    layer.endPoint = CGPointMake(1.0, 0);
}

/**
 设置渐变
 
 @param benginColor 开始颜色
 @param endColor 结束颜色
 @param frame 大小
 @return CAGradientLayer
 */
+ (CAGradientLayer *)setDbModuleListJianBianWithBenginColor:(UIColor *)benginColor
                                                   endColor:(UIColor *)endColor
                                                      frame:(CGRect)frame {
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)benginColor.CGColor, (__bridge id)endColor.CGColor];
    gradientLayer.locations = @[@0.0, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1.0);
    gradientLayer.frame = frame;
    return gradientLayer;
}


/**
 给View某一边画线

 @param view view
 @param top 上
 @param left 左
 @param bottom 下
 @param right 右
 @param color 颜色
 @param width 宽
 */
+ (void)setBorderWithView:(UIView *)view
                      top:(BOOL)top
                     left:(BOOL)left
                   bottom:(BOOL)bottom
                    right:(BOOL)right
              borderColor:(UIColor *)color
              borderWidth:(CGFloat)width {
    
    if (top) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, view.frame.size.width, width);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
    if (left) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, width, view.frame.size.height);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
    if (bottom) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, view.frame.size.height - width, view.frame.size.width, width);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
    if (right) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(view.frame.size.width - width, 0, width, view.frame.size.height);
        layer.backgroundColor = color.CGColor;
        [view.layer addSublayer:layer];
    }
}

@end
